from captcha.fields import CaptchaField
from django import forms

from .models import CustomUser


class ChangePwdForm(forms.Form):
    password = forms.CharField(label='原密码', widget=forms.PasswordInput(attrs={
        'class': 'form-control'

    }))

    password1 = forms.CharField(label='新密码', widget=forms.PasswordInput(attrs={
        'class': 'form-control'

    }))

    password2 = forms.CharField(label='确认新密码', widget=forms.PasswordInput(attrs={
        'class': 'form-control'

    }))

    def clean_password1(self):
        password = self.cleaned_data.get('password')
        password1 = self.cleaned_data.get('password1')
        if len(password) < 6:
            raise forms.ValidationError("密码长度小于6")

        if password1 == password:
            raise forms.ValidationError("与原密码一致，修改失败")

        return password1

    def clean_password2(self):
        pwd = self.cleaned_data.get('password1')
        pwd2 = self.cleaned_data.get('password2')

        if pwd2 != pwd:
            raise forms.ValidationError("密码不一致！")
        return pwd2


class LoginForm(forms.Form):
    username = forms.CharField(label='用户名', widget=forms.TextInput(attrs={
        'class': 'form-control'

    }))
    password = forms.CharField(label='密码', widget=forms.PasswordInput(attrs={
        'class': 'form-control'

    }))




class RegisForm(forms.ModelForm):
    password = forms.CharField(label='确认密码', widget=forms.PasswordInput(attrs={
        'class': 'form-control'
    }))
    password2 = forms.CharField(label='确认密码', widget=forms.PasswordInput(attrs={
        'class': 'form-control'
    }))

    captcha = CaptchaField(label='验证码')
    class Meta:
        model = CustomUser
        fields = ('username', 'password')
        widgets = {
            'username': forms.TextInput(attrs={'class': 'form-control'}),
            'password': forms.PasswordInput(attrs={'class': 'form-control'}),
        }

    def clean_password(self):
        password = self.cleaned_data.get('password')
        if len(password) < 6:
            raise forms.ValidationError("密码长度小于6")
        else:
            return password

    def clean_password2(self):
        pwd = self.cleaned_data.get('password')
        pwd2 = self.cleaned_data.get('password2')
        if pwd2 != pwd:
            raise forms.ValidationError("密码不一致！")
        return pwd2



class ChangeInfoForm(forms.ModelForm):
    class Meta:
        model = CustomUser
        fields = ('username','header')
        widgets = {
            'username': forms.TextInput(attrs={
                'class': 'form-control'
            }),
        }

        labels = {
            'header':'头像'
        }

    def clean_header(self):
        header = self.cleaned_data.get('header')
        return header

class ChangeHeadForm(forms.ModelForm):
    class Meta:
        model = CustomUser
        fields = ('header',)
        labels = {
            'header': '头像'
        }

    def clean_header(self):
        header = self.cleaned_data.get('header')
        if header:
            # 检查文件大小 (限制为5MB)
            if header.size > 5 * 1024 * 1024:
                raise forms.ValidationError("头像文件不能超过5MB")
            # 检查文件类型
            if not header.content_type.startswith('image/'):
                raise forms.ValidationError("请上传图片文件")
        return header




